//store an input within the program


#[derive(Clone)]
pub struct Rotor {
    //polyalphabetic cypher, notch location, start position
    pub leftalph: Vec<u8>,
    pub notch: u8,
}
impl Rotor {
    pub fn new(rotorconf: &str, ring: u8 ) ->Rotor {
        //parse rotor strings, ring sets rotation

        let x: Vec<&str> = rotorconf.split_whitespace().collect();
        let notch: u8 = x[1].parse().unwrap();
        let mut leftalph: Vec<u8> = Vec::new();
        let alphraw:Vec<char> = x[0].chars().collect();

        //set alphabet rotation relative to notch, convert from String to Vec<u8>
        for i in ring as usize .. alphraw.len() {
            leftalph.push(alphraw[i].to_owned() as u8 - 65);
        }
        for i in 0 .. ring as usize {
            leftalph.push(alphraw[i].to_owned() as u8 - 65);
        }
        //confirm correct array length
        if leftalph.len() != 26 {
           panic!("Incorrect length on rotor {}. Should be 26 but is {}.\n", notch, leftalph.len());
        }

        Rotor {
            leftalph: leftalph,
            notch: notch,
        }
    }

    pub fn step(&mut self) {
        let x = self.leftalph.clone();
        for i in 1..26 {
            self.leftalph[i] = x[i-1];
        }
        self.leftalph[0] = x[25];
        self.notch += 1;
        if self.notch == 26 {
            self.notch = 0;
        }
        else if self.notch > 26 {
            panic!("notch outside acceptable range.");
        }
        //step left side of rotors
        for i in 0 .. 26 {
            self.leftalph[i] += 1;
            if self.leftalph[i] == 26 {
                self.leftalph[i] = 0;
            }
            if self.leftalph[i] > 26 {
                panic!("rotor character out of range!");
            }
        }
    }
    pub fn as_string(&self) -> String{
        let mut abc = "".to_string();
        for i in 0 .. 26 {
            abc.push((self.leftalph[i]+65) as char);
        }
        abc

    }
}

